﻿<?php require_once('Connections/satphp.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE disciplina SET idturma=%s, nome=%s, cargahoraria=%s, datainicio=%s, datafim=%s WHERE iddisciplina=%s",
                       GetSQLValueString($_POST['idturma'], "int"),
                       GetSQLValueString($_POST['nome'], "text"),
                       GetSQLValueString($_POST['cargahoraria'], "int"),
                       GetSQLValueString($_POST['datainicio'], "date"),
                       GetSQLValueString($_POST['datafim'], "date"),
                       GetSQLValueString($_POST['iddisciplina'], "int"));

  mysql_select_db($database_satphp, $satphp);
  $Result1 = mysql_query($updateSQL, $satphp) or die(mysql_error());

  $updateGoTo = "disciplina-listar.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_satphp, $satphp);
$query_RSdisciplina = "SELECT * FROM disciplina WHERE iddisciplina = '$id'";
$RSdisciplina = mysql_query($query_RSdisciplina, $satphp) or die(mysql_error());
$row_RSdisciplina = mysql_fetch_assoc($RSdisciplina);
$totalRows_RSdisciplina = mysql_num_rows($RSdisciplina);

mysql_select_db($database_satphp, $satphp);
$query_RSturma = "SELECT * FROM turma";
$RSturma = mysql_query($query_RSturma, $satphp) or die(mysql_error());
$row_RSturma = mysql_fetch_assoc($RSturma);
$totalRows_RSturma = mysql_num_rows($RSturma);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
</head>

<body>
<p align="center"><strong>Editar disciplina</strong></p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Idturma:</td>
      <td><select name="idturma">
        <?php 
do {  
?>
        <option value="<?php echo $row_RSturma['idturma']?>" <?php if (!(strcmp($row_RSturma['idturma'], htmlentities($row_RSdisciplina['idturma'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_RSturma['nome']?></option>
        <?php
} while ($row_RSturma = mysql_fetch_assoc($RSturma));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Nome:</td>
      <td><input type="text" name="nome" value="<?php echo htmlentities($row_RSdisciplina['nome'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Cargahoraria:</td>
      <td><input type="text" name="cargahoraria" value="<?php echo htmlentities($row_RSdisciplina['cargahoraria'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Datainicio:</td>
      <td><input type="text" name="datainicio" value="<?php echo htmlentities($row_RSdisciplina['datainicio'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Datafim:</td>
      <td><input type="text" name="datafim" value="<?php echo htmlentities($row_RSdisciplina['datafim'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Atualizar registro" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="iddisciplina" value="<?php echo $row_RSdisciplina['iddisciplina']; ?>" />
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($RSdisciplina);

mysql_free_result($RSturma);
?>
